package com.blgroup.oa.configuration;

import org.activiti.engine.*;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @author TLB17
 */
@Configuration
@Import(DruidConfig.class)
public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration {

    @Autowired
    DataSource dataSource;
    @Autowired
    PlatformTransactionManager transactionManager;


//    @Bean
//    public SpringProcessEngineConfiguration springProcessEngineConfiguration(
//            DataSource dataSource,
//            PlatformTransactionManager transactionManager,
//            SpringAsyncExecutor springAsyncExecutor) throws IOException {
//        return this.baseSpringProcessEngineConfiguration(dataSource, transactionManager, springAsyncExecutor);
//    }


    @Bean
    public SpringProcessEngineConfiguration getProcessEngineConfiguration() {
        SpringProcessEngineConfiguration pec = new SpringProcessEngineConfiguration();
        pec.setDataSource(dataSource);
        pec.setTransactionManager(transactionManager);
        pec.setDatabaseSchemaUpdate("true");
        return pec;
    }

    @Bean
    public ProcessEngineFactoryBean getProcessEngine() {
        ProcessEngineFactoryBean pefb = new ProcessEngineFactoryBean();
        pefb.setProcessEngineConfiguration(getProcessEngineConfiguration());
        return pefb;
    }


    @Bean
    public IdentityService identityService() throws Exception {
        return getProcessEngine().getObject().getIdentityService();
    }

    @Bean
    public RepositoryService repositoryService() throws Exception {
        return getProcessEngine().getObject().getRepositoryService();
    }

    @Bean
    public TaskService taskService() throws Exception {
        return getProcessEngine().getObject().getTaskService();
    }

    @Bean
    public RuntimeService runtimeService() throws Exception {
        return getProcessEngine().getObject().getRuntimeService();
    }

    @Bean
    public HistoryService historyService() throws Exception {
        return getProcessEngine().getObject().getHistoryService();
    }

    @Bean
    public FormService formService() throws Exception {
        return getProcessEngine().getObject().getFormService();
    }

    @Bean
    public ManagementService managementService() throws Exception {
        return getProcessEngine().getObject().getManagementService();
    }

}
